import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import HomeView from '../views/HomeView.vue'
import EchartsView from '../views/components/EchartsView.vue'
import PermissionList from '../views/components/PermissionList.vue'
import UserOne from '../views/components/UserOne.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: "/login"
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: '/echarts',
        name: 'echarts',
        component: EchartsView,
        meta: {
          title: "数据展示"
        }
      },
      {
        path: '/permissionlist',
        name: 'permissionlist',
        component: PermissionList,
        meta: {
          title: "权限列表"
        }
      },
      {
        path: '/user',
        name: 'user',
        component: UserOne,
        redirect:"/userlist",
        meta: {
          title: "住户信息"
        },
        children: [
          {
            path: '/userlist',
            name: 'userlist',
            component: () => import('../views/components/UserList.vue'),
            meta: {
              //分类标题
              classifyTitle: "用户信息",
              title: "住户列表"
            }
          },
          {
            path: '/userupdate',
            name: 'userupdate',
            component: () => import('../views/components/UserUpdate.vue'),
            meta: {
              //分类标题
              classifyTitle: "用户信息",
              title: "住户信息修改"
            }
          }
        ]
      },
    ]
  },

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 路由守卫
router.beforeEach((to,from,next)=>{
   // 需要登录才能访问的 路由地址集合
  const arr=[
    '/home',
    '/echarts',
    '/permissionlist',
    '/user',
    '/userupdate',
    '/userlist'
  ]
  if(!arr.includes(to.path)){
    next()
  }else{
    if(localStorage.getItem('token')){
      next()
    }else{
      next({
        path:'login',
        query:{
          refer:to.fullPath
        }
      })
    }
  }
})
export default router
